package com.rd.student.jiaji.class2.shirui.testbank;

import java.util.Objects;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestBank implements Runnable {

    // 银行类需要有账户来取钱
    // Bank has a Account  构成一个类的组合关系
    Account account;
    // 取钱的金额
    double money;

    public TestBank(String name, Account account, double money) {

        this.account = account;
        this.money = money;
    }


    // 1
//    @Override
//    public void run() {
//        synchronized (account) {
//            if (account.getBalance() < money) {
//                System.out.println("余额不足");
//                return;
//            }
//
//            System.out.println(Thread.currentThread().getName() + " 可以取钱");
//            account.setBalance(account.getBalance() - money);
//            System.out.println("取钱后的余额为：" + account.getBalance());
//        }
//    }

    // 2
//    @Override
//    public void run() {
//        synchronized (Account.class) {
//            if (account.getBalance() < money) {
//                System.out.println("余额不足");
//                return;
//            }
//
//            System.out.println(Thread.currentThread().getName() + " 可以取钱");
//            account.setBalance(account.getBalance() - money);
//            System.out.println("取钱后的余额为：" + account.getBalance());
//        }
//    }

    //3 ReentrantLock 锁

    private static Lock lock = new ReentrantLock();

    public void run() {

        lock.lock();

        if (account.getBalance() < money) {
            System.out.println("余额不足");
            return;
        }

        System.out.println(Thread.currentThread().getName() + " 可以取钱");
        account.setBalance(account.getBalance() - money);
        System.out.println("取钱后的余额为：" + account.getBalance());

        lock.unlock();

    }


    public static void main(String[] args) {
        Account account = new Account("123456", 888);
//        new TestBank("zhangsan", account, 666).start();
//        new TestBank("lisi", account, 666).start();
        Thread thread1 = new Thread(new TestBank("zhangsan", account, 666));
        Thread thread2 = new Thread(new TestBank("lisi", account, 666));
        thread1.start();
        thread2.start();
    }


    // 创建一个账户类
    static class Account {
        // 属性设置为账户id和余额
        String accountId;
        double balance;


        // 创建构造器 ctrl+N
        // 选择 constructor构造器 按住shift选择两个属性 ok
        public Account(String accountId, double balance) {
            this.accountId = accountId;
            this.balance = balance;
        }

        public double getBalance() {
            return balance;
        }

        public void setBalance(double balance) {
            this.balance = balance;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Account account = (Account) o;
            return Objects.equals(accountId, account.accountId);
        }

        @Override
        public int hashCode() {
            return Objects.hash(accountId);
        }


    }
}